package com.itqian.controller;


import com.itqian.common.exception.EorderException;
import com.itqian.common.utils.ResultVOUtil;
import com.itqian.common.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shishuqian
 * date 2022/1/12
 * time 20:08
 **/
@Slf4j
@RestControllerAdvice
public class UnifiedException {


    /**
     *
     * @author shishuqian
     * @desc 对实体类的参数异常进行统一的处理
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultVO handleException(MethodArgumentNotValidException e) {
        List<String> violations = e.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage).
                collect(Collectors.toList());
        return ResultVOUtil.fail(violations.toString());
    }


    /**
     *
     * @author shishuqian
     * @desc  eorder异常统一处理
     */
    @ExceptionHandler(value = EorderException.class)
    public ResultVO handlerException(EorderException e){
        log.info("服务器内部异常，{}", e.getMessage());
        e.printStackTrace();
        return ResultVOUtil.fail(e.getMessage());
    }
}
